<?php

reset ($_FILES);

include_once("server/library.php");

uses("server_httpRequest");

$uploadClassName = $httpRequest->getParameter("uploadClassName");
$funcName = $httpRequest->getParameter("funcName");

$param1 = $httpRequest->getParameter("param1");
$param2 = $httpRequest->getParameter("param2");
$param3 = $httpRequest->getParameter("param3");
$param4 = $httpRequest->getParameter("param4");
$resId = $httpRequest->getParameter("resId");

$ret = false;
uses("server_Response");
$response = new server_Response();
try
{
	$tmp = __FILE__;			
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
		$dirSeparator = "\\";
	else
		$dirSeparator = "/";	
	if ($param4 == "gridupload"){			
		$filename = strtolower($_FILES["uploadFile"]['name']);
		$tmpFile = $_FILES["uploadFile"]['tmp_name'];		
		for ($i = 1; $i < 3; $i++)
		{	
			$pos = strrpos($tmp, $dirSeparator);
			$tmp = substr($tmp, 0, $pos);
		}
		$ext = strpos($filename,".zip");		
		if (!$ext)
		{
			$ext = strpos($filename,".txt");			
			if 	(!$ext)	
			{
				$ext = strpos($filename,".xml");
				if (!$ext){
					$ext = strpos($filename,".xls");		
					if (!$ext)
						die ("unreadable file");					
					uses("server_util_excel");					
					$xls = new server_util_excel($file2);					
					$data = $xls->read2Txt($tmpFile);					
					$ret = true;					
					//$param3 = "text";					
				}else {
					uses("server_xml_Doc",false);		
					$xml = new XML_Doc();					
					$xml->parseFile($tmpFile);				
					$data = xml_data($xml->rootNode);				
					$ret = true;
				}
			}else 
			{								
					$file = $tmpFile;							
					$data = file_get_contents($file);					
					$ret = true;
			}
		}else 
		{ 
			$zip = 	zip_open($tmpFile);
			if ($zip) 
			{
				while ($zip_entry = zip_read($zip)) 
				{
					$fname = zip_entry_name($zip_entry);
					if (zip_entry_open($zip, $zip_entry, "r")) 
					{
						$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
						zip_entry_close($zip_entry);
						$xml = new XML_Doc();					
						$xml->parse($buf);
						$data = xml_data($xml->rootNode);
						$data = substr($data,1);
						echo $data;
						echo "<br>";
					}		
				}			
				zip_close($zip);
				$ret = true;
			}
		}
		if ($ret && $param3=="text"){			
			$valueList = explode("\r\n", $data);
			$header = explode("\t",$valueList[0]);									
			$headerText = "";			
			foreach ($header as $c =>$value){															
				$headerText .= ";" . $value;					
			}
			$headerText = substr($headerText,1);
			$result = "\r\n";			
			for ($i = 1; $i < count($valueList)-1;$i++){
				$line = explode("\t",$valueList[$i]);				
				$data = "";
				foreach ($line as $c =>$value){															
					$data .= ";" . $value;					
				}
				$data = substr($data,1);
				$result .= $data . "\n";
			}			
			$map = $headerText . $result ."\r\n". ($i-1);
			$response->setCode(0);
			$response->setValue($map);
		}else if ($ret && $param3=="xls"){			
			$response->setCode(0);
			$response->setValue($data);
		}else{
			$mapheader = $param3;
			
			uses("server_util_Map");
			$map = new server_util_Map();		
			$valueList = explode("\r\n", $data);
			$header = explode("\t",$valueList[0]);			
			$fieldDesc = new server_util_Map();							
			$desc1 = new server_util_Map();
			$desc2 = new server_util_Map();
			if ($mapheader != null) $header = explode(";",$mapheader);
			for ($i = 1; $i < count($valueList)-1;$i++){
				$line = explode("\t",$valueList[$i]);
				$lineMap = new server_util_Map();
				foreach ($header as $c =>$value){															
					$lineMap->set($value,$line[$c]);
					$desc1->set($value,250);
					$desc2->set($value,"S");
				}
				$map->set(($i - 1),$lineMap);
			}
			$fieldDesc->set(0,$desc1);
			$fieldDesc->set(1,$desc2);
			$map->setTag1(($i-1));
			$map->setTag2($fieldDesc);		
			$response->setCode(0);
			$response->setValue($map);
		 }
	}else if ($param4 == "data"  ){
		$filename = strtolower($_FILES["uploadFile"]['name']);
		$tmpFile = $_FILES["uploadFile"]['tmp_name'];
		$ret = false;
		if ($_FILES['uploadFile']['size'] > 1024000){ 
			$response->setCode(1);
			$response->setValue("File terlalu besar ( harus < 1 mb)." );			
			throw new Exception("File terlalu besar " . $_FILES['uploadFile']['size']);
			$ret = false;
		}		
		if ($param3 == "image"){	
			if ($_FILES['uploadFile']['type'] == "image/gif") $ret = true;
			elseif ($_FILES['uploadFile']['type'] == "image/jpg") $ret = true; 
			elseif ($_FILES['uploadFile']['type'] == "image/jpeg") $ret = true; 
			elseif ($_FILES['uploadFile']['type'] == "image/bmp") $ret = true; 
			elseif ($_FILES['uploadFile']['type'] == "image/png") $ret = true; 
			else {
				$response->setCode(1);
				$response->setValue("Bukan file gambar " . $_FILES['uploadFile']['type']);			
				throw new Exception("Bukan File gambar " . $_FILES['uploadFile']['type']);
			}		
		}else {
			$ret = true;
		}
		if ($ret){
			$pos = strrpos($tmp, $dirSeparator);
			$tmp = substr($tmp, 0, $pos);		
			$pos = strrpos($filename, ".");
			$ext = substr($filename,$pos + 1, strlen($filename) - $pos);
			uses("server_util_Map");
			$map = new server_util_Map();
			$map->set("filename",$filename);
			$map->set("tanggal",date ("d/m/Y H:i:s",filemtime($tmpFile)));
			$map->set("folder","media");
			$map->set("tipe",$_FILES['uploadFile']['type']);
			$map->set("size",$_FILES['uploadFile']['size']);
			$map->set("ext",$ext);
			$filename = $param1 . "_" . md5(uniqid(rand(), true)) . "." .$ext;	
			$map->set("tmpfile",$tmp. $dirSeparator ."server". $dirSeparator . "tmp" . $dirSeparator . $filename);
			$response->setCode(0);
			$response->setValue($map);
				
			$data = file_get_contents($tmpFile);
			$save = "server". $dirSeparator . "tmp" . $dirSeparator . $filename;			
			file_put_contents($save, $data);
		}
	}else if ($param1 == "uploadTo"  ){
		$tmpFile = $_FILES["uploadFile"]['tmp_name'];	
		$filename = strtolower($_FILES["uploadFile"]['name']);		
		$ret = copy($tmpFile, $param2 . $filename );			
	}else {
	    uses($uploadClassName);
	    $obj = new $uploadClassName();
		while (list ($clave, $val) = each ($_FILES)) 
		{	
			$ret = $obj->$funcName($val['name'], $val['tmp_name'], $param1, $param2, $param3, $param4);
		}
	}
}
catch (Exception $e)
{
    error_log($e->getTraceAsString());
    $ret = false;
}

if ($ret)
    $retVal = "true";
else
    $retVal = "false";
?>
<script>	
    window.parent.page.getResource(<?php echo($resId); ?>).doUploadFinished(<?php echo($retVal); ?>,<?php echo($response->toXML()); ?>);
</script>
